﻿namespace IPMS.BLL
{
    using IPMS.DAL;
    using System;
    using System.Data;
    using System.Data.SqlClient;

    public class Country
    {
        private string _ChineseName;
        private string _Country;
        private string _EnglishName;
        private bool _PCTYN;
        private bool _IsDelete;

        public int GetCountryInfo(string country)
        {
            SqlCommand sqlCmd = new SqlCommand();
            DataAccessLayer DAL = new DataAccessLayer();
            DAL.AddParamToSQLCmd(sqlCmd, "@ReturnValue", SqlDbType.Int, 0, ParameterDirection.ReturnValue, null);
            DAL.AddParamToSQLCmd(sqlCmd, "@Country", SqlDbType.NVarChar, 100, ParameterDirection.Input, country.ToString());
            DAL.AddParamToSQLCmd(sqlCmd, "@ChineseName", SqlDbType.NVarChar, 100, ParameterDirection.Output, null);
            DAL.AddParamToSQLCmd(sqlCmd, "@EnglishName", SqlDbType.NVarChar, 100, ParameterDirection.Output, null);
            DAL.AddParamToSQLCmd(sqlCmd, "@PCTYN", SqlDbType.Bit, 1, ParameterDirection.Output, null);
            DAL.AddParamToSQLCmd(sqlCmd, "@IsDelete", SqlDbType.Bit, 1, ParameterDirection.Output, null);
            DAL.SetCommandType(sqlCmd, CommandType.StoredProcedure, "RetrieveCountrySp");
            DAL.ExecuteScalarCmd(sqlCmd);
            int resultValue = (int) sqlCmd.Parameters["@ReturnValue"].Value;
            this._ChineseName = (string) sqlCmd.Parameters["@ChineseName"].Value;
            this._EnglishName = (string) sqlCmd.Parameters["@EnglishName"].Value;
            this._PCTYN = (bool) sqlCmd.Parameters["@PCTYN"].Value;
            this._IsDelete = Convert.ToBoolean(sqlCmd.Parameters["@IsDelete"].Value);
            return resultValue;
        }

        public bool GetPCT(string Country)
        {
            SqlCommand sqlCmd = new SqlCommand();
            DataAccessLayer DAL = new DataAccessLayer();
            DAL.AddParamToSQLCmd(sqlCmd, "@ReturnValue", SqlDbType.Int, 0, ParameterDirection.ReturnValue, null);
            DAL.AddParamToSQLCmd(sqlCmd, "@Country", SqlDbType.NVarChar, 0xff, ParameterDirection.Input, Country.ToString());
            DAL.AddParamToSQLCmd(sqlCmd, "@PCT", SqlDbType.Bit, 1, ParameterDirection.Output, 0);
            DAL.SetCommandType(sqlCmd, CommandType.StoredProcedure, "GetCountryPCTSp");
            DAL.ExecuteScalarCmd(sqlCmd);
            int resultValue = (int) sqlCmd.Parameters["@ReturnValue"].Value;
            try
            {
                return (bool) sqlCmd.Parameters["@PCT"].Value;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public bool Update(bool IsDelete, string UpdateBy, string UpdateIP, string country)
        {
            bool isSuccess = false;
            try
            {
                SqlCommand sqlCommand = new SqlCommand();
                DataAccessLayer DAL = new DataAccessLayer();

                string sqlString = "UPDATE [Country] SET IsDelete = @IsDelete,UpdateBy = @UpdateBy,UpdateDate = GETDATE(), UpdateIP = @UpdateIP WHERE (([Country] = @Country)) ";

                DAL.SetCommandType(sqlCommand, CommandType.Text, sqlString);
                DAL.AddParamToSQLCmd(sqlCommand, "@IsDelete", SqlDbType.Bit, 1, ParameterDirection.Input, IsDelete);
                DAL.AddParamToSQLCmd(sqlCommand, "@UpdateBy", SqlDbType.NVarChar, 60, ParameterDirection.Input, UpdateBy);
                DAL.AddParamToSQLCmd(sqlCommand, "@UpdateIP", SqlDbType.NVarChar, 50, ParameterDirection.Input, UpdateIP);
                DAL.AddParamToSQLCmd(sqlCommand, "@Country", SqlDbType.NVarChar, 60, ParameterDirection.Input, country);
                DAL.ExecuteScalarCmd(sqlCommand);

                isSuccess = true;
            }
            catch { }

            return isSuccess;
        }

        public string ChineseName
        {
            get
            {
                return this._ChineseName;
            }
        }

        public string EnglishName
        {
            get
            {
                return this._EnglishName;
            }
        }

        public bool PCTYN
        {
            get
            {
                return this._PCTYN;
            }
        }

        public bool IsDelete
        {
            get
            {
                return this._IsDelete;
            }
            set
            {
                this._IsDelete = value;
            }
        }
    }
}

